package com.music.glide.load

import androidx.collection.ArrayMap
import androidx.collection.SimpleArrayMap
import com.music.glide.util.KCacheHashCodeArrayMap
import java.security.MessageDigest
import java.security.cert.PKIXRevocationChecker.Option
import java.util.Optional

/**
 *
 * 用于应用到内存和磁盘缓存键的一组选项。
 */
class KOptions :KKey{

    private val values: ArrayMap<KOption<*>, Any> = KCacheHashCodeArrayMap()

    fun putAll(other: KOptions) {
        values.putAll(other.values as SimpleArrayMap<out KOption<*>, out Any>)
    }

    fun <T> set(option: KOption<T>, value: T) {
        values[option] = value
    }

    fun <T> get(option:KOption<T>):T?{
        return (values[option] as? T) ?: option.getDefaultValue()
    }


    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        values.forEach {
            updateDiskCacheKey(it.key, it.value, messageDigest)
        }
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as KOptions

        if (values != other.values) return false

        return true
    }

    override fun hashCode(): Int {
        return values.hashCode()
    }

    companion object{
        fun <T> updateDiskCacheKey(option:KOption<T>,value:Any,messageDigest: MessageDigest) {
            option.update(value as T, messageDigest)
        }
    }
}